package com.tangly.module.controller;

import com.github.pagehelper.PageInfo;
import com.tangly.common.base.BaseController;
import com.tangly.module.entity.CmsArticleEntity;
import com.tangly.module.entity.MessageLogEntity;
import com.tangly.module.service.IMessageLogService;
import com.tangly.module.vo.AppCmsArticleVO;
import com.tangly.module.vo.AppMessageLogVO;
import com.tangly.module.vo.ResultVO;
import com.tangly.module.vo.SearchRequestVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * created by yehao on 2018/7/27.
 */
@RestController
@RequestMapping(value = "/app")
@Slf4j
@Api(tags = "获取消息列表")
public class AppMessageLogController extends BaseController {

    @Autowired
    private IMessageLogService iMessageLogService;
    @ApiOperation(value = "获取消息列表", notes = "获取列表,在url中使用?page=xx&size=xx实现分页,不传默认是1,10")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", paramType = "query", value = "页码(不传默认是1)", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "size", paramType = "query", value = "页面大小(不传默认是10)", dataType = "int", defaultValue = "10"),
            @ApiImplicitParam(name = "sorts", paramType = "query", value = "排序字段，排序条件 例：[+id,-name]表示根据id 正序排列并根据name逆向排序;"),
            @ApiImplicitParam(name = "keyword", paramType = "query", value = "查询关键字,通常会根据name字段模糊查询")
    })
    @GetMapping(value = "msg")
    public ResultVO<AppMessageLogVO> getList(
            @RequestParam(name = "page", defaultValue = "1", required = false) Integer page,
            @RequestParam(name = "size", defaultValue = "10", required = false) Integer size,
            @RequestParam(name = "sorts", required = false) String[] sorts,
            @RequestParam(name = "keyword", required = false) String keyword
    ) {
        SearchRequestVO searchRequestVO = new SearchRequestVO(page, size, sorts);
        if (!StringUtils.isEmpty(keyword)) {
            searchRequestVO.addColumn("summary", "%" + keyword + "%");
        }
        PageInfo<MessageLogEntity> pages = iMessageLogService.selectOrByPage(searchRequestVO, MessageLogEntity.class);
        List<AppMessageLogVO> list = new ArrayList<>();
        for (MessageLogEntity messageLogEntity : pages.getList()) {
            list.add(AppMessageLogVO.convert(messageLogEntity));
        }
        return new ResultVO<>(pages.getTotal(), pages.getPages(), list);

    }

}
